package com.evil.common.activiti.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 回退类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum FallbackTypeEnum {

    /**
     * 回退类型，1: 之前所有节点, 2: 上一节点, 3: 指定节点
     */
    ALL_BEFORE(1, "之前所有节点"),
    ONE_BEFORE(2, "上一节点"),
    ASSIGN_BEFORE(3, "指定节点"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<FallbackTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static FallbackTypeEnum isExistById(Integer id) {
        return FallbackTypeEnum.findById(id).orElseThrow(() ->
                new BusinessException(RCodeEnum.ERROR_PARAMETER, "回退类型参数错误"));
    }
}
